import { gettaskReportInfo, getOrderCount, getOrderAmount, getSkuTopList, getAmountCollect, getRegionCollect } from '@/api/home'
export default {
  namespaced: true,
  state: {
    TasksInfo: [],
    orderAmount: 0,
    orderCount: 0,
    topList: [],
    amountInfo: {},
    regionInfo: {}
  },
  mutations: {
    setTasksInfo(state, val) {
      state.TasksInfo = val
    },
    setOrdersInfo(state, val) {
      state.orderCount = val[0]
      state.orderAmount = val[1]
    },
    setTopList(state, list) {
      state.topList = list
    },
    setAmountInfo(state, info) {
      state.amountInfo = info
    },
    setRegionInfo(state, info) {
      state.regionInfo = info
    }
  },
  actions: {
    async getTasksInfo({ commit }, params) {
      try {
        const res = await gettaskReportInfo(params)
        // console.log(res)
        commit('setTasksInfo', res)
      } catch (error) {
        throw new Error(error)
      }
    },
    async getOrdersInfo({ commit }, params) {
      try {
        const count = await getOrderCount(params)
        const amount = await getOrderAmount(params)
        commit('setOrdersInfo', [count, amount])
      } catch (error) {
        throw new Error(error)
      }
    },
    async getTopList({ commit }, params) {
      try {
        const res = await getSkuTopList(params)
        commit('setTopList', res)
      } catch (error) {
        throw new Error(error)
      }
    },
    async getAmountInfo({ commit }, params) {
      try {
        const res = await getAmountCollect(params)
        commit('setAmountInfo', res)
      } catch (error) {
        throw new Error(error)
      }
    },
    async getRegionInfo({ commit }, params) {
      try {
        const res = await getRegionCollect(params)
        commit('setRegionInfo', res)
      } catch (error) {
        throw new Error(error)
      }
    }
  },
  getters: {
    totalWorkerCounts(state) {
      return state.TasksInfo.reduce((prev, it) => prev + it.total, 0)
    },
    completedTotal(state) {
      return state.TasksInfo.reduce((prev, it) => prev + it.completedTotal, 0)
    },
    progressTotal(state) {
      return state.TasksInfo.reduce((prev, it) => prev + it.progressTotal, 0)
    },
    cancelTotal(state) {
      return state.TasksInfo.reduce((prev, it) => prev + it.cancelTotal, 0)
    }
  }
}
